package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Provider;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.ProviderService;
import com.cch.demo.bus.vo.GoodsVo;
import com.cch.demo.sys.util.AppFileUtils;
import com.cch.demo.sys.util.Constast;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-11
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsServiceImpl;

    @Resource
    private ProviderService providerServiceImpl;

    @RequestMapping("/loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo goodsVo){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        //先判断null 怕provideid中没有值
        queryWrapper.eq(goodsVo.getProviderid()!=null&&goodsVo.getProviderid()!=0,"providerid",goodsVo.getProviderid());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize());

        IPage<Goods> page = new Page<>(goodsVo.getPage(),goodsVo.getLimit());
        goodsServiceImpl.page(page,queryWrapper);

        //给goods中供应商添加名字
        List<Goods> goods  = page.getRecords();
        for(Goods good:goods){
            Integer providerid = good.getProviderid();
            Provider provider =  providerServiceImpl.getById(providerid);
            if(provider!=null){
                good.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),goods);
    }

    @RequestMapping("addGoods")
    public ResultObj addGoods(GoodsVo goodsVo){
        try{

            //判断是否为默认图片
            String imager = goodsVo.getGoodsimg();
            if(null!=imager && imager.endsWith("_temp")){
                //去掉图片后缀
                String newimager = AppFileUtils.renameFile(imager);

                goodsVo.setGoodsimg(newimager);
            }
            goodsServiceImpl.save(goodsVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("updateGoods")
    public ResultObj updateGoods(GoodsVo goodsVo){
        try{
            //判断是否为默认图片
            if(!(null!=goodsVo.getGoodsimg()&&goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMAGER))){
                //判断是否为新的图片
                if(goodsVo.getGoodsimg().endsWith("_temp")){
                    //1 默认图片 换 新图片
                    //2 旧图片 换 新图片

                    //去掉图片后缀
                    String newname = AppFileUtils.renameFile(goodsVo.getGoodsimg());
                    goodsVo.setGoodsimg(newname);
                    //获取原始图片名字
                    String oldname = goodsServiceImpl.getById(goodsVo.getId()).getGoodsimg();
                    if(!oldname.equals(Constast.DEFAULT_IMAGER)){
                        //如果原来的不是默认图片 则删除原来的图片
                        AppFileUtils.removeFile(oldname);
                    }
                }
            }
            goodsServiceImpl.updateById(goodsVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteGoods")
    public ResultObj deleteGoods(Integer id,String goodsimg){
        try{
            if(!goodsimg.equals(Constast.DEFAULT_IMAGER)){
                //如果原来的不是默认图片 则删除原来的图片
                AppFileUtils.removeFile(goodsimg);
            }
            goodsServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteGoods")
    public ResultObj BatchDeleteGoods(GoodsVo goodsVo,String[] paths){
        try{
            for(String path:paths){
                if(!path.equals(Constast.DEFAULT_IMAGER)){
                    //如果原来的不是默认图片 则删除原来的图片
                    AppFileUtils.removeFile(path);
                }
            }
            List<Serializable> idlist = new ArrayList<>();
            for (Integer id:goodsVo.getIds()){
                idlist.add(id);
            }
            goodsServiceImpl.removeByIds(idlist);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("/loadAllGoodsForSelect")
    public DataGridView loadAllGoodsForSelect(){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        //先判断null 怕provideid中没有值
        queryWrapper.eq("available",Constast.AVAILABLE_TRUE);
        List<Goods> goods = goodsServiceImpl.list(queryWrapper);
        //给goods中供应商添加名字
        for(Goods good:goods){
            Integer providerid = good.getProviderid();
            Provider provider =  providerServiceImpl.getById(providerid);
            if(provider!=null){
                good.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(goods);
    }

    //根据供应商ID查询商品
    @RequestMapping("loadGoodsByProviderId")
    public DataGridView loadGoodsByProviderId(Integer providerid){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("providerid",providerid);
        List<Goods> list = goodsServiceImpl.list(queryWrapper);
        return new DataGridView(list);
    }

    //根据goodsId查询goods
    @RequestMapping("loadGoodsByGoodId")
    public Goods loadGoodsByGoodsId(Integer goodsid){
        Goods goods = goodsServiceImpl.getById(goodsid);
        return goods;
    }

}

